package me.zhengjie.gas.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.gas.dto.TransactionDataDto;
import me.zhengjie.gas.model.ResponseDto;
import me.zhengjie.gas.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/transaction")
@Api(tags = "油站：交易数据接口")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;


    @ApiOperation("新增交易")
    @PostMapping("/data")
    public ResponseDto data(@RequestBody TransactionDataDto data) {
        return transactionService.newTransaction(data);
    }

    @ApiOperation("獲得所有交易UUID")
    @GetMapping("/allUuid")
    public ResponseDto listAllUuid(@RequestParam("deptId") Long deptId,
                                   @RequestParam(value = "startDate", required = false) String startDate,
                                   @RequestParam(value = "endDate", required = false) String endDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return transactionService.listAllUuid(deptId, LocalDateTime.parse(startDate, formatter),
                LocalDateTime.parse(endDate, formatter));
    }

    @ApiOperation("查詢交易")
    @GetMapping("/all")
    public ResponseDto listAll(@RequestParam("startDate") String startDate,
                               @RequestParam("endDate") String endDate,
                               @RequestParam(value = "deptId", required = false) Long deptId,
                               @RequestParam(value = "lineNo", required = false) Integer lineNo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return transactionService.listAll(LocalDateTime.parse(startDate, formatter),
                LocalDateTime.parse(endDate, formatter), deptId, lineNo);
    }

    @ApiOperation("查詢本部門交易")
    @GetMapping("/byDept")
    public ResponseDto deptAll(@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                               @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex) {
        return transactionService.deptAll(pageSize, pageIndex);
    }

    @ApiOperation("更加車牌號查詢交易")
    @GetMapping("/byLicenseNo")
    public ResponseDto findTransactionByLicenseNo(@RequestParam("licenseNo") String licenseNo) {
        return transactionService.findTransactionByLicenseNo(licenseNo);
    }
}
